from util.Route import *
from service import DictManageService
# 获取所有的字典(分页 所有信息)
@route("/dict/get/all",methods = ['POST'])
def dictGetAll():
    data = request.json 
    num = data.get("num") or 1
    size = data.get("size") or 10
    cdn = data.get('cdn') or {}
    page = Page(num=num, size=size)  # Create a Page object
    r = DictManageService.getDictAll(page,cdn)
    return R.success(r)


# 根据path 和type查询字典的值
@route("/dict/search/kv")
def dicPathGetKv():
    path:str = request.args.get("path")  # Create a Page object
    type:str = request.args.get("type")
    r = DictManageService.getDictPathTypeKv(path,type)
    return R.success(r)

# 修改字典
@route("/dict/update", methods = ['POST'])
def dictUpdate():
    k:dict = request.json
    r = DictManageService.updateDict(k)
    if r:
        return R.success(r,"字典修改成功")
    else:
        return R.create(400,None,'字典修改失败')
    
    
# 删除字典
@route("/dict/delete", methods = ['POST'])
def dictDelete():
    dids = request.json.get('ids')
    r = DictManageService.delDict(dids)
    if r:
        return R.success(r,"字典删除成功")
    else:
        return R.create(400,None,'字典删除失败')


# 添加字典（除了did）
@route("/dict/add", methods = ['POST'])
def dictAdd():
    data:dict = request.json
    # 定义默认值
    data = formatParam(data,['cId'])
    r = DictManageService.addDict(data)
    if r:
        return R.success(r,"·字典添加成功")
    else:
        return R.create(400,r,'字典添加失败字典路径与类型重复')
 
    
# 更新字典键值
@route("/dict/update/all_key_val", methods = ['POST'])
def editDictKeyVal():
    body = request.json
    r = DictManageService.updateAllKeyVal(body['data'],body['dId'])
    return R.success(r,'更新成功')
    
# 根据Did获取某字典所有的键值
@route("/dict/get_key_val_by_id")
def getDictKeyVal():
    id = request.args.get("dId")
    return R.success(DictManageService.getDictAllKeyVal(id))